package ru.aslanov.schedule.model;

import ru.aslanov.schedule.shared.DayOfWeek;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Created by IntelliJ IDEA.
 * Created: Nov 25, 2009 5:58:31 PM
 *
 * @author Sergey Aslanov
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ScheduleItem extends Entity implements Comparable<ScheduleItem> {
    @Persistent
    private DayOfWeek dayOfWeek;
    @Persistent
    private byte dayOfWeekInt;
    @Persistent
    private String startTime;
    @Persistent
    private String endTime;
    @NotPersistent
    private UnownReference<Location> location = new UnownReference<Location>(Location.class);

    @Persistent
    private String googleEventId;
    @Persistent
    private Group group;

    public ScheduleItem() {
    }

    public ScheduleItem(DayOfWeek dayOfWeek, String startTime, String endTime, Location location, String googleEventId) {
        setDayOfWeek(dayOfWeek);
        this.startTime = startTime;
        this.endTime = endTime;
        this.location.setObject(location);
        this.googleEventId = googleEventId;
    }
    
    @XmlAttribute(name = "day")
    public DayOfWeek getDayOfWeek() {
        return dayOfWeek;
    }

    @XmlAttribute
    @XmlSchemaType(name="time")
    public String getStartTime() {
        return startTime;
    }

    @XmlAttribute
    @XmlSchemaType(name="time")
    public String getEndTime() {
        return endTime;
    }

    @XmlAttribute(name = "loc")
    @XmlIDREF
    public Location getLocation() {
        getLocationKey();
        return location.getObject();
    }

    public Location getLocation(PersistenceManager pm) {
        getLocationKey();
        return location.getObject(pm);
    }

    public void setDayOfWeek(DayOfWeek dayOfWeek) {
        this.dayOfWeek = dayOfWeek;
        if (dayOfWeek != null) {
            this.dayOfWeekInt = (byte) dayOfWeek.ordinal();
        }
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public void setLocation(Location location) {
        this.location.setObject(location);
    }

    @XmlAttribute
    public String getGoogleEventId() {
        return googleEventId;
    }

    public void setGoogleEventId(String googleEventId) {
        this.googleEventId = googleEventId;
    }

    @XmlTransient
    @Persistent
    public String getLocationKey() {
        return location.getEncodedKey();
    }

    public void setLocationKey(String locationKey) {
        this.location.setEncodedKey(locationKey);
    }

    @XmlTransient
    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public String toString() {
        return "ScheduleItem{" +
                "dayOfWeek=" + dayOfWeek +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", location=" + location +
                '}';
    }

    @Override
    public int compareTo(ScheduleItem o) {
        return dayOfWeek != o.dayOfWeek ? dayOfWeek.compareTo(o.dayOfWeek) : startTime.compareTo(o.startTime);
    }
}
